(function() {
  var body_dropdown_items, check_precheck, crypto, do_encrypt, do_input_1, do_input_3, export_mpos, express, fs, iced, input_precheck, jobs, mkdirp, mmm, moment, multer, path, pgsql, router, secure, update_precheck, uploader, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  moment = require('moment');

  fs = require('fs');

  multer = require('multer');

  mmm = require('mmmagic');

  mkdirp = require('mkdirp');

  crypto = require('crypto');

  path = require('path');

  jobs = require('../lib/terms_jobs');

  secure = require('../lib/secure');

  router = express.Router();

  module.exports = router;

  router.use(function(req, res, next) {
    var k, _i, _len, _ref;
    _ref = ['merid', 'mapto_term', 'tmk', 'input_time', 'check_time', 'effect_time'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k] || req.body[k].length === 0) {
        req.body[k] = null;
      }
    }
    return next();
  });

  router.all('/', function(req, res, next) {
    var client, done, err, ex_count, result, ret_records, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    where_clause = "( t.serial::text like $1 or t.uuid like $1 or t.termid like $1 or t.tmk like $1 or t.notes like $1 or m.merno like $1 or m.name like $1 )";
    if (req.body.inside && req.body.inside.length > 0) {
      where_clause += " and m.inside = " + (req.body.inside === 'true');
      res.locals.inside = req.body.inside;
    }
    if (req.body.chnl && req.body.chnl.length > 0) {
      where_clause += " and m.chnl = " + req.body.chnl;
      res.locals.chnl = req.body.chnl;
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 46
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select count(t.uuid)::int as count from pay_terms as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and t.shaded = false", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 56
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          ex_count = 0;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select count(t.uuid)::int as count from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and t.inputer = $2", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 71
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            ex_count += result.rows[0].count;
            (function(__iced_k) {
              if (res.locals.page_perms.check) {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                  });
                  client.query("select count(t.uuid)::int as count from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and t.inputer <> $2 and t.checked = false and t.rejected = false", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 85
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  return __iced_k(ex_count += result.rows[0].count);
                });
              } else {
                return __iced_k();
              }
            })(function() {
              (function(__iced_k) {
                if (res.locals.page_perms.check) {
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                    });
                    client.query("select count(t.uuid)::int as count from pay_terms_ex as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and t.checker = $2 and t.checked = true", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid], __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          err = arguments[0];
                          return result = arguments[1];
                        };
                      })(),
                      lineno: 99
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    if (err) {
                      done(client);
                      return next(err);
                    }
                    return __iced_k(ex_count += result.rows[0].count);
                  });
                } else {
                  return __iced_k();
                }
              })(function() {
                res.locals.total += ex_count;
                ret_records = [];
                (function(__iced_k) {
                  if (res.locals.offset === 0 && res.locals.page_perms.check) {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                      });
                      client.query("select t.*, u.realname as input_user, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, t2.termid as mapto_term_id, true as ex from pay_terms_ex as t left join web_users as u on u.uuid = t.inputer left join pay_merch as m on m.uuid = t.merid left join pay_terms as t2 on t2.uuid = t.mapto_term where " + where_clause + " and t.inputer <> $2 and t.checked = false and t.rejected = false order by t.serial desc limit $3::int", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid, res.locals.page_rows - ret_records.length], __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return result = arguments[1];
                          };
                        })(),
                        lineno: 124
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      if (err) {
                        done(client);
                        return next(err);
                      }
                      return __iced_k(ret_records.push.apply(ret_records, result.rows));
                    });
                  } else {
                    return __iced_k();
                  }
                })(function() {
                  (function(__iced_k) {
                    if (res.locals.offset === 0 && res.locals.page_perms.check && ret_records.length < res.locals.page_rows) {
                      (function(__iced_k) {
                        __iced_deferrals = new iced.Deferrals(__iced_k, {
                          parent: ___iced_passed_deferral,
                          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                        });
                        client.query("select t.*, u.realname as input_user, u2.realname as check_user, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, t2.termid as mapto_term_id, true as ex from pay_terms_ex as t left join web_users as u on u.uuid = t.inputer left join web_users as u2 on u2.uuid = t.checker left join pay_merch as m on m.uuid = t.merid left join pay_terms as t2 on t2.uuid = t.mapto_term where " + where_clause + " and t.checker = $2 and t.checked = true order by t.serial desc limit $3::int", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid, res.locals.page_rows - ret_records.length], __iced_deferrals.defer({
                          assign_fn: (function() {
                            return function() {
                              err = arguments[0];
                              return result = arguments[1];
                            };
                          })(),
                          lineno: 149
                        }));
                        __iced_deferrals._fulfill();
                      })(function() {
                        if (err) {
                          done(client);
                          return next(err);
                        }
                        return __iced_k(ret_records.push.apply(ret_records, result.rows));
                      });
                    } else {
                      return __iced_k();
                    }
                  })(function() {
                    (function(__iced_k) {
                      if (res.locals.offset === 0 && ret_records.length < res.locals.page_rows) {
                        (function(__iced_k) {
                          __iced_deferrals = new iced.Deferrals(__iced_k, {
                            parent: ___iced_passed_deferral,
                            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                          });
                          client.query("select t.*, u.realname as input_user, u2.realname as check_user, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, t2.termid as mapto_term_id, true as ex from pay_terms_ex as t left join web_users as u on u.uuid = t.inputer left join web_users as u2 on u2.uuid = t.checker left join pay_merch as m on m.uuid = t.merid left join pay_terms as t2 on t2.uuid = t.mapto_term where " + where_clause + " and t.inputer = $2 order by t.serial desc limit $3::int", ['%' + res.locals.keyword + '%', req.session.userinfo.uuid, res.locals.page_rows - ret_records.length], __iced_deferrals.defer({
                            assign_fn: (function() {
                              return function() {
                                err = arguments[0];
                                return result = arguments[1];
                              };
                            })(),
                            lineno: 172
                          }));
                          __iced_deferrals._fulfill();
                        })(function() {
                          if (err) {
                            done(client);
                            return next(err);
                          }
                          return __iced_k(ret_records.push.apply(ret_records, result.rows));
                        });
                      } else {
                        return __iced_k();
                      }
                    })(function() {
                      (function(__iced_k) {
                        if (ret_records.length < res.locals.page_rows) {
                          (function(__iced_k) {
                            __iced_deferrals = new iced.Deferrals(__iced_k, {
                              parent: ___iced_passed_deferral,
                              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                            });
                            client.query("select t.*, u.realname as input_user, u2.realname as check_user, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, m.disabled as mer_disabled, m.shaded as mer_shaded, t2.termid as mapto_term_id from pay_terms as t left join web_users as u on u.uuid = t.inputer left join web_users as u2 on u2.uuid = t.checker left join pay_merch as m on m.uuid = t.merid left join pay_terms as t2 on t2.uuid = t.mapto_term where " + where_clause + " and t.shaded = false order by t.serial desc offset $2::int limit $3::int", ['%' + res.locals.keyword + '%', res.locals.offset === 0 ? 0 : res.locals.offset - ex_count, res.locals.page_rows - ret_records.length], __iced_deferrals.defer({
                              assign_fn: (function() {
                                return function() {
                                  err = arguments[0];
                                  return result = arguments[1];
                                };
                              })(),
                              lineno: 197
                            }));
                            __iced_deferrals._fulfill();
                          })(function() {
                            if (err) {
                              done(client);
                              return next(err);
                            }
                            return __iced_k(ret_records.push.apply(ret_records, result.rows));
                          });
                        } else {
                          return __iced_k();
                        }
                      })(function() {
                        done();
                        return res.render('terms/index', {
                          records: ret_records
                        });
                      });
                    });
                  });
                });
              });
            });
          });
        });
      };
    })(this));
  });

  body_dropdown_items = function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 207
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        if (req.path.startsWith('/check/3')) {
          where_clause = '1 = 1';
        } else {
          where_clause = 'disabled = false';
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_merch where " + where_clause + " and chnl in (1, 4) order by ctime desc, merno", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 220
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.merids = result.rows;
          done();
          return next();
        });
      };
    })(this));
  };

  router.get('/new', body_dropdown_items, function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '新增终端'
    });
    return res.render('terms/new', {
      record: {}
    });
  });

  update_precheck = function(req, res, next) {
    var client, done, err, k, merch, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['merid', 'termid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    req.body.termid = req.body.termid.replace(/\s/g, '');
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 240
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_merch where uuid = $1", [req.body.merid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 245
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error("隶属商户 " + req.body.merid + " 不存在，请检查"));
          }
          merch = result.rows[0];
          if (merch.inside && !req.body.tmk) {
            return next(new Error("平台终端需要配置终端主密钥"));
          }
          if (req.body.tmk && req.body.tmk !== req.body.tmk_old) {
            req.body.tmk = secure.encryptEde(req.body.tmk);
            if (!req.body.tmk) {
              return next(new Error('加密终端主密钥失败'));
            }
          }
          req.app.set('notify_modified_time', moment().format('x'));
          return next();
        });
      };
    })(this));
  };

  router.post('/new', update_precheck, function(req, res, next) {
    var client, done, err, result, term_uuid, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 269
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms where merid = $1 and termid = $2", [req.body.merid, req.body.termid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 276
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length > 0) {
            done();
            return next(new Error("商户 " + req.body.merid + " 下的终端 " + req.body.termid + " 已存在，请检查"));
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select t.*, u.realname as input_user from pay_terms_ex as t left join web_users as u on u.uuid = t.inputer where t.merid = $1 and t.termid = $2 and t.action = 1", [req.body.merid, req.body.termid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 290
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            if (result.rows.length > 0) {
              done();
              return next(new Error(("终端 " + req.body.termid + " 已被用户 ") + ("" + result.rows[0].input_user + " 添加，尚未复核或尚未生效")));
            }
            term_uuid = uuid.v4();
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
              });
              client.query("insert into pay_terms ( uuid, termid, merid, inputer, checker, shaded ) values ( $1, $2, $3, $4, $5, true )", [term_uuid, req.body.termid, req.body.merid, req.session.userinfo.uuid, req.session.userinfo.uuid], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 307
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                });
                client.query("insert into pay_terms_ex ( uuid,       termid,     merid, mapto_term, tmk,        macalgo, notes,      ref_uuid, inputer,    input_time,     action ) values ( $1, $2, $3, $4, $5, $6, $7, $8, $9, CURRENT_TIMESTAMP, 1 )", [uuid.v4(), req.body.termid, req.body.merid, req.body.mapto_term, req.body.tmk, parseInt(req.body.macalgo) || 0, req.body.notes, term_uuid, req.session.userinfo.uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 331
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                done();
                return res.redirect('/terms');
              });
            });
          });
        });
      };
    })(this));
  });

  router.get('/edit/:id', body_dropdown_items, function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '编辑终端信息'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 340
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms where serial = $1::int or uuid = $2", [parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 347
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error("未查询到终端 " + req.params.id + " 的信息"));
          }
          record = result.rows[0];
          return res.render('terms/edit', {
            record: record
          });
        });
      };
    })(this));
  });

  router.post('/edit', update_precheck, function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 362
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms_ex where ref_uuid = $1", [req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 368
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length > 0) {
            done();
            return next(new Error('该记录被用户修改后尚未生效，在新信息生效前不允许再次修改'));
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("insert into pay_terms_ex ( uuid,       termid, merid, mapto_term, tmk,    macalgo, notes, ref_uuid,          ref_serial, inputer,           input_time,     action ) values ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, CURRENT_TIMESTAMP, 2 )", [uuid.v4(), req.body.termid, req.body.merid, req.body.mapto_term, req.body.tmk, parseInt(req.body.macalgo) || 0, req.body.notes, req.body.uuid, req.body.serial, req.session.userinfo.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 397
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.redirect('/terms');
          });
        });
      };
    })(this));
  });

  router.post('/disable', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid', 'disabled'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 408
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms set disabled = $1 where serial = $2 and uuid = $3", [req.body.disabled, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 415
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/terms'
          });
        });
      };
    })(this));
  });

  router.post('/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 426
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms_ex where ref_uuid = $1", [req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 433
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length > 0) {
            done();
            return next(new Error('该记录被用户修改后尚未生效，在新信息生效前不允许删除'));
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("insert into pay_terms_ex ( uuid,           ctime, termid,         merid, mapto_term, tmk,            macalgo, notes, ref_uuid,       ref_serial, inputer,        input_time,     action ) ( select $1,             ctime, termid,         merid, mapto_term, tmk,            macalgo, notes, uuid,           serial, $2,             CURRENT_TIMESTAMP, 3 from pay_terms where uuid = $3 and serial = $4 )", [uuid.v4(), req.session.userinfo.uuid, req.body.uuid, req.body.serial], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 460
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            req.app.set('notify_modified_time', moment().format('x'));
            return res.json({
              succ: true,
              redirect: '/terms'
            });
          });
        });
      };
    })(this));
  });

  router.get('/input/:id', body_dropdown_items, function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '修改终端信息'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 472
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.*, u.realname as check_user from pay_terms_ex as t left join web_users as u on u.uuid = t.checker where (t.serial = $1::int or t.uuid = $2) and t.inputer = $3", [parseInt(req.params.id) || 0, req.params.id, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 482
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error("未查询到终端 " + req.params.id + " 的信息"));
          }
          record = result.rows[0];
          if (record.checked) {
            return next(new Error("该记录已被用户 " + record.check_user + " 复核，不允许继续修改"));
          }
          return res.render('terms/input', {
            record: record
          });
        });
      };
    })(this));
  });

  router.post('/input/delete', function(req, res, next) {
    var client, done, err, k, remove_record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 502
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms_ex where inputer = $1 and checked = false and serial = $2 and uuid = $3", [req.session.userinfo.uuid, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 510
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            done();
            return next(new Error('未查询到待删除记录，可能已被删除.'));
          }
          remove_record = result.rows[0];
          client.query('begin');
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("delete from pay_terms_ex where inputer = $1 and checked = false and serial = $2 and uuid = $3", [req.session.userinfo.uuid, req.body.serial, req.body.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 524
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            (function(__iced_k) {
              if (remove_record.action === 1) {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                  });
                  client.query("delete from pay_terms where shaded = true and uuid = $1", [remove_record.ref_uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 532
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  return __iced_k();
                });
              } else {
                return __iced_k();
              }
            })(function() {
              client.query('commit');
              done();
              req.app.set('notify_modified_time', moment().format('x'));
              return res.json({
                succ: true,
                redirect: '/terms'
              });
            });
          });
        });
      };
    })(this));
  });

  input_precheck = function(req, res, next) {
    var client, done, err, k, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 547
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.checked, u.realname as check_user from pay_terms_ex as t left join web_users as u on u.uuid = t.checker where t.serial = $1 and t.uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 557
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error("未找到终端 " + req.body.uuid + " 的信息，可能已被复核并已生效"));
          }
          record = result.rows[0];
          if (result.rows[0].checked) {
            return next(new Error("该终端信息已被用户 " + record.check_user + " 复核，不允许继续修改"));
          }
          return next();
        });
      };
    })(this));
  };

  do_input_1 = function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 573
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms_ex set termid = $1,         merid = $2, mapto_term = $3, tmk = $4,            macalgo = $5, notes = $6, inputer = $7,        input_time = CURRENT_TIMESTAMP, checked = false,     rejected = false where serial = $8 and uuid = $9", [req.body.termid, req.body.merid, req.body.mapto_term, req.body.tmk, parseInt(req.body.macalgo) || 0, req.body.notes, req.session.userinfo.uuid, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 592
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          return done();
        });
      };
    })(this));
  };

  do_input_3 = function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 598
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms_ex set inputer = $1,        input_time = CURRENT_TIMESTAMP, checked = false,     rejected = false where serial = $2 and uuid = $3", [req.session.userinfo.uuid, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 607
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          return done();
        });
      };
    })(this));
  };

  router.post('/input/:action', update_precheck, input_precheck, function(req, res, next) {
    switch (parseInt(req.params.action)) {
      case 1:
      case 2:
        do_input_1(req, res, next);
        break;
      case 3:
        do_input_3(req, res, next);
        break;
      default:
        next(new Error('您访问的功能不存在'));
    }
    return res.redirect('/terms');
  });

  router.get('/check/1/:id', body_dropdown_items, function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '确认新增终端'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 624
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.*, u.realname as input_user, u2.realname as check_user from pay_terms_ex as t left join web_users as u on t.inputer = u.uuid left join web_users as u2 on t.checker = u2.uuid where (t.serial = $1 or t.uuid = $2) and t.inputer <> $3 and action = 1", [parseInt(req.params.id) || 0, req.params.id, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 636
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error('未找到待复核终端记录'));
          }
          record = result.rows[0];
          return res.render('terms/check1', {
            record: record
          });
        });
      };
    })(this));
  });

  router.get('/check/2/:id', function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '确认变更终端信息'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 649
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.*, u.realname as input_user, u2.realname as check_user, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, t2.termid as mapto_term_id from pay_terms_ex as t left join web_users as u on t.inputer = u.uuid left join web_users as u2 on t.checker = u2.uuid left join pay_merch as m on t.merid = m.uuid left join pay_terms as t2 on t.mapto_term = t2.uuid where (t.serial = $1 or t.uuid = $2) and t.inputer <> $3 and action = 2", [parseInt(req.params.id) || 0, req.params.id, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 666
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            done();
            return next(new Error('未找到终端记录'));
          }
          record = result.rows[0];
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select t.*, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl, t2.termid as mapto_term_id from pay_terms as t left join pay_merch as m on t.merid = m.uuid left join pay_terms as t2 on t.mapto_term = t2.uuid where t.uuid = $1", [record.ref_uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 685
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            if (result.rows.length === 0) {
              return next(new Error('未找到原终端记录'));
            }
            res.locals.ref_record = result.rows[0];
            return res.render('terms/check2', {
              record: record
            });
          });
        });
      };
    })(this));
  });

  router.get('/check/3/:id', body_dropdown_items, function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '确认删除终端'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 699
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.*, u.realname as input_user, u2.realname as check_user from pay_terms_ex as t left join web_users as u on t.inputer = u.uuid left join web_users as u2 on t.checker = u2.uuid where (t.serial = $1 or t.uuid = $2) and t.inputer <> $3 and action = 3", [parseInt(req.params.id) || 0, req.params.id, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 711
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error('未找到终端记录'));
          }
          record = result.rows[0];
          return res.render('terms/check3', {
            record: record
          });
        });
      };
    })(this));
  });

  check_precheck = function(req, res, next) {
    var client, done, err, k, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid', 'input_time'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 727
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.*, u.realname as input_user, u2.realname as check_user from pay_terms_ex as t left join web_users as u on u.uuid = t.inputer left join web_users as u2 on u2.uuid = t.checker where t.serial = $1 and t.uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 737
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error('未查询到待复核记录'));
          }
          record = result.rows[0];
          if (record.checked && record.checker !== req.session.userinfo.uuid) {
            return next(new Error('用户 #{record.check_user} 已经复核了此记录'));
          }
          if (record.input_time.isAfter(req.body.input_time, 'second')) {
            return next(new Error("在复核期间用户 " + record.input_user + " 修改了原终端信息，请刷新后重新复核"));
          }
          req.app.set('notify_modified_time', moment().format('x'));
          return next();
        });
      };
    })(this));
  };

  router.post('/check/pass', check_precheck, function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (!req.body.effect_time) {
      return next(new Error('请输入信息生效时间'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 764
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms_ex set checked = true, checker = $1, check_time = CURRENT_TIMESTAMP, effect_time = $2, rejected = false, reject_reason = '' where serial = $3 and uuid = $4", [req.session.userinfo.uuid, req.body.effect_time, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 775
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          jobs.sched_effect(req.body.uuid, req.body.effect_time);
          return res.redirect('/terms');
        });
      };
    })(this));
  });

  router.post('/check/reject', check_precheck, function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (!req.body.reject_reason || req.body.reject_reason.length === 0) {
      return next(new Error('请输入拒绝原因'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 788
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms_ex set checked = false, checker = $1, check_time = CURRENT_TIMESTAMP, rejected = true, reject_reason = $2 where serial = $3 and uuid = $4", [req.session.userinfo.uuid, req.body.reject_reason.trim(), req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 799
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/terms');
        });
      };
    })(this));
  });

  router.get('/batch/edit', function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '批量修改'
    });
    return res.render('terms/batch_edit', {
      record: {}
    });
  });

  router.post('/batch/edit', function(req, res, next) {
    res.type('json');
    return res.json({
      succ: true,
      redirect: '/terms'
    });
  });

  router.post('/batch/check', function(req, res, next) {
    var client, done, err, index, item, k, params, records, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _j, _len, _len1, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['records'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    records = JSON.parse(req.body.records);
    if (records.length === 0) {
      return {
        succ: true,
        redirect: '/terms'
      };
    }
    params = [];
    params.push(req.session.userinfo.uuid);
    where_clause = "uuid in (";
    for (index = _j = 0, _len1 = records.length; _j < _len1; index = ++_j) {
      item = records[index];
      where_clause += "$" + (index + 2);
      if (index < records.length - 1) {
        where_clause += ", ";
      }
      params.push(item.uuid);
    }
    where_clause += ")";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 835
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms_ex set checked = true, checker = $1, check_time = CURRENT_TIMESTAMP, effect_time = CURRENT_TIMESTAMP, rejected = false, reject_reason = '' where " + where_clause, params, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 845
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _k, _len2;
          if (err) {
            done(client);
            return next(err);
          }
          done();
          for (_k = 0, _len2 = records.length; _k < _len2; _k++) {
            item = records[_k];
            jobs.sched_effect(item.uuid, moment());
          }
          req.app.set('notify_modified_time', moment().format('x'));
          return res.json({
            succ: true,
            redirect: '/terms'
          });
        });
      };
    })(this));
  });

  router.post('/batch/disable', function(req, res, next) {
    var client, done, err, index, item, k, params, records, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _j, _len, _len1, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['records'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    records = JSON.parse(req.body.records);
    if (records.length === 0) {
      return {
        succ: true,
        redirect: '/terms'
      };
    }
    params = [];
    where_clause = "uuid in (";
    for (index = _j = 0, _len1 = records.length; _j < _len1; index = ++_j) {
      item = records[index];
      where_clause += "$" + (index + 1);
      if (index < records.length - 1) {
        where_clause += ", ";
      }
      params.push(item.uuid);
    }
    where_clause += ")";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 876
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms set disabled = true where " + where_clause, params, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 881
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          req.app.set('notify_modified_time', moment().format('x'));
          return res.json({
            succ: true,
            redirect: '/terms'
          });
        });
      };
    })(this));
  });

  router.post('/batch/enable', function(req, res, next) {
    var client, done, err, index, item, k, params, records, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _j, _len, _len1, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['records'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    records = JSON.parse(req.body.records);
    if (records.length === 0) {
      return {
        succ: true,
        redirect: '/terms'
      };
    }
    params = [];
    where_clause = "uuid in (";
    for (index = _j = 0, _len1 = records.length; _j < _len1; index = ++_j) {
      item = records[index];
      where_clause += "$" + (index + 1);
      if (index < records.length - 1) {
        where_clause += ", ";
      }
      params.push(item.uuid);
    }
    where_clause += ")";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 908
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("update pay_terms set disabled = false where " + where_clause, params, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 913
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          req.app.set('notify_modified_time', moment().format('x'));
          return res.json({
            succ: true,
            redirect: '/terms'
          });
        });
      };
    })(this));
  });

  router.post('/batch/delete', function(req, res, next) {
    var client, done, err, index, item, k, records, result, uuids, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _j, _len, _len1, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['records'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    records = JSON.parse(req.body.records);
    uuids = "(";
    for (index = _j = 0, _len1 = records.length; _j < _len1; index = ++_j) {
      item = records[index];
      uuids += "'" + item.uuid + "'";
      if (index < records.length - 1) {
        uuids += ", ";
      }
    }
    uuids += ")";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 936
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select * from pay_terms where mapto_term in " + uuids, __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 942
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length > 0) {
            done();
            return next(new Error("记录中存在正被其它终端引用的记录，在解除引用关系之前不允许删除"));
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select * from pay_terms_ex where mapto_term in " + uuids, __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 952
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            if (result.rows.length > 0) {
              done();
              return next(new Error("记录中存在正被待复核表中的其它终端引用的记录，在解除引用关系之前不允许删除"));
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
              });
              client.query("select * from pay_terms_ex where ref_uuid in " + uuids, __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 961
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              if (result.rows.length > 0) {
                done();
                return next(new Error("记录中有被用户修改后尚未生效的记录，在新信息生效前不允许删除"));
              }
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                });
                client.query("insert into pay_terms_ex ( uuid,           ctime, termid,         merid, mapto_term, tmk,            macalgo, notes, ref_uuid,       ref_serial, inputer,        input_time,     action ) ( select md5(random()::text || clock_timestamp()::text)::uuid::varchar, ctime, termid,         merid, mapto_term, tmk,            macalgo, notes, uuid,           serial, $1,             CURRENT_TIMESTAMP, 3 from pay_terms where uuid in " + uuids + " )", [req.session.userinfo.uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 985
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                done();
                req.app.set('notify_modified_time', moment().format('x'));
                return res.json({
                  succ: true,
                  redirect: '/terms'
                });
              });
            });
          });
        });
      };
    })(this));
  });

  router.post('/batch/delete_ex', function(req, res, next) {
    var client, done, err, index, item, k, records, ref_uuids, result, uuids, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _j, _len, _len1, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['records'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    records = JSON.parse(req.body.records);
    uuids = "(";
    for (index = _j = 0, _len1 = records.length; _j < _len1; index = ++_j) {
      item = records[index];
      uuids += "'" + item.uuid + "'";
      if (index < records.length - 1) {
        uuids += ", ";
      }
    }
    uuids += ")";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 1008
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select ref_uuid from pay_terms_ex where inputer = $1 and uuid in " + uuids, [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 1015
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _k, _len2, _ref1;
          if (err) {
            done(client);
            return next(err);
          }
          ref_uuids = "(";
          _ref1 = result.rows;
          for (index = _k = 0, _len2 = _ref1.length; _k < _len2; index = ++_k) {
            item = _ref1[index];
            ref_uuids += "'" + item.ref_uuid + "'";
            if (index < records.length - 1) {
              ref_uuids += ", ";
            }
          }
          ref_uuids += ")";
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query('begin');
            __iced_deferrals._fulfill();
          })(function() {
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
              });
              client.query("delete from pay_terms_ex where checked = false and inputer = $1 and uuid in " + uuids, [req.session.userinfo.uuid], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 1031
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                });
                client.query("delete from pay_terms where shaded = true and inputer = $1 and uuid in " + ref_uuids, [req.session.userinfo.uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 1038
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
                  });
                  client.query('commit');
                  __iced_deferrals._fulfill();
                })(function() {
                  done();
                  req.app.set('notify_modified_time', moment().format('x'));
                  return res.json({
                    succ: true,
                    redirect: '/terms'
                  });
                });
              });
            });
          });
        });
      };
    })(this));
  });

  router.get('/import', function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '导入商户信息'
    });
    return res.render('terms/import', {
      record: {}
    });
  });

  uploader = multer({
    dest: settings.upload_path
  });

  router.post('/import', uploader.single('attach_file'), function(req, res, next) {
    var dest_path, err, k, magic, result, today, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['inside', 'chnl'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (!req.file) {
      return next(new Error("请求缺少上传文件."));
    }
    magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        magic.detectFile(req.file.path, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              return result = arguments[1];
            };
          })(),
          lineno: 1064
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          fs.unlink(req.file.path);
          return next(new Error('不能检测文件类型，导入失败'));
        }
        if (result !== 'text/plain') {
          fs.unlink(req.file.path);
          return next(new Error('上传的文件格式非文本文件，无效'));
        }
        if (req.body.inside === 'true') {
          dest_path = "" + settings.upload_path + "/terms/" + req.body.chnl + "/inner";
        } else {
          dest_path = "" + settings.upload_path + "/terms/" + req.body.chnl + "/outer";
        }
        mkdirp.sync(dest_path);
        today = moment().format('YYYY-MM-DD');
        fs.rename(req.file.path, "" + dest_path + "/" + today + ".txt");
        return res.redirect('/terms');
      };
    })(this));
  });

  router.get('/export', function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '导出终端信息'
    });
    res.locals.keyword = req.query.keyword;
    where_clause = "( t.termid like $1 or t.tmk like $1 or t.notes like $1 or m.merno like $1 or m.name like $1 ) and t.shaded = false and t.disabled = false";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 1093
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select count(t.*) as count from pay_terms as t left join pay_merch as m on m.uuid = t.merid where " + where_clause, ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 1102
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.export_count = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select count(t.*) as count from pay_terms as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and m.inside = true", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 1112
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.inside_count = result.rows[0].count;
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
              });
              client.query("select t.*, m.merno from pay_terms as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and m.inside = true and (t.tmk is null or t.tmk = '')", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 1122
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              res.locals.notmk_records = result.rows;
              done();
              return res.render('terms/export', {
                record: {}
              });
            });
          });
        });
      };
    })(this));
  });

  do_encrypt = function(tmk_clear, encrypt_key) {
    var cipher, tmk_cipher;
    cipher = crypto.createCipheriv('des-ede', encrypt_key, new Buffer(''));
    cipher.setAutoPadding(false);
    tmk_cipher = cipher.update(tmk_clear, 'hex', 'hex');
    tmk_cipher += cipher.final('hex');
    return tmk_cipher.toUpperCase();
  };

  export_mpos = function(req, res, next, where_clause) {
    var cipher_tmk, client, dirname, dnfile, done, encrypt_key, err, i, key1, key2, lines, mpos_skey1, mpos_skey2, record, result, secure_conf, tmk, today, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 1139
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select mpos_skey1, mpos_skey2 from pay_secure where 1 = 1", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 1144
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            done();
            return next(new Error('尚未配置母 POS 密钥分量，请在安全配置中进行配置.'));
          }
          secure_conf = result.rows[0];
          if (secure_conf.mpos_skey1.length !== 32 || secure_conf.mpos_skey2.length !== 32) {
            done();
            return next(new Error('母 POS 密钥分量配置错误，请检查安全配置.'));
          }
          mpos_skey1 = secure.decryptEde(secure_conf.mpos_skey1);
          mpos_skey2 = secure.decryptEde(secure_conf.mpos_skey2);
          if (!mpos_skey1 || !mpos_skey2) {
            done();
            return next(new Error('解密母 POS 密钥分量错误.'));
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
            });
            client.query("select m.merno as merno, t.termid, t.tmk from pay_terms as t left join pay_merch as m on m.uuid = t.merid where " + where_clause + " and m.inside = true and m.chnl = 1 order by merno, t.termid", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 1172
            }));
            __iced_deferrals._fulfill();
          })(function() {
            var _i, _j, _len, _ref, _ref1;
            if (err) {
              done(client);
              return next(err);
            }
            done();
            if (result.rows.length === 0) {
              return next(new Error('没有可导出的记录，请检查...'));
            }
            encrypt_key = new Buffer(16);
            key1 = Buffer.from(mpos_skey1, 'hex');
            key2 = Buffer.from(mpos_skey2, 'hex');
            for (i = _i = 0, _ref = key1.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
              encrypt_key[i] = key1[i] ^ key2[i];
            }
            lines = [];
            _ref1 = result.rows;
            for (_j = 0, _len = _ref1.length; _j < _len; _j++) {
              record = _ref1[_j];
              if (!record.tmk || record.tmk.length !== 32) {
                return next(new Error("终端[" + record.merno + "][" + record.termid + "]没有配置终端密钥，请检查."));
              }
              tmk = secure.decryptEde(record.tmk);
              if (!tmk) {
                return next(new Error("解密终端[" + record.merno + "][" + record.termid + "]主密钥失败."));
              }
              cipher_tmk = do_encrypt(tmk, encrypt_key) + Array(17).join(' ');
              lines.push(("\"" + record.merno + "\",\"" + record.termid + "\",") + ("\"0814473610   \",\"" + cipher_tmk + "\",\"" + cipher_tmk + "\""));
            }
            dirname = settings.dnload_path + "/terms";
            mkdirp.sync(dirname);
            today = moment().format('YYYY-MM-DD');
            dnfile = "" + dirname + "/terms-keys-" + today + ".txt";
            fs.writeFileSync(dnfile, lines.join('\n'));
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
              });
              res.download(dnfile, path.basename(dnfile), __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    return err = arguments[0];
                  };
                })(),
                lineno: 1210
              }));
              __iced_deferrals._fulfill();
            })(function() {
              fs.unlink(dnfile);
              return res.end();
            });
          });
        });
      };
    })(this));
  };

  router.post('/export', function(req, res, next) {
    var k, where_clause, _i, _len, _ref;
    _ref = ['format'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    res.locals.keyword = req.body.keyword;
    where_clause = "( t.termid like $1 or t.tmk like $1 or t.notes like $1 or m.merno like $1 or m.name like $1 ) and t.shaded = false and t.disabled = false";
    if (req.body.format === 'mpos') {
      return export_mpos(req, res, next, where_clause);
    }
    return next(new Error('导出文件格式无效.'));
  });

  router.post('/query/mapto_terms', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['chnl'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 1240
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.uuid, t.termid, t.shaded, m.name as mer_name, m.merno as mer_no, '" + constant.merch_chnls[req.body.chnl] + "' as mer_chnl from pay_terms as t left join pay_merch as m on m.uuid = t.merid where t.disabled = false and m.inside = false and m.chnl = $1", [req.body.chnl], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 1253
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            records: result.rows
          });
        });
      };
    })(this));
  });

  router.post('/query/mapto_term_merch', function(req, res, next) {
    var client, done, err, k, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['mapto_term'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 1267
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/terms.coffee"
          });
          client.query("select t.termid, m.name as mer_name, m.merno as mer_no, m.inside as mer_inside, m.chnl as mer_chnl from pay_terms as t left join pay_merch as m on m.uuid = t.merid where t.disabled = false and t.uuid = $1", [req.body.mapto_term], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 1279
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          record = result.rows[0];
          if (!record) {
            return res.json({
              succ: false,
              errmsg: '未查询到商户信息'
            });
          }
          record.mer_chnl = constant.merch_chnls[record.mer_chnl];
          return res.json({
            succ: true,
            record: record
          });
        });
      };
    })(this));
  });

}).call(this);
